<template>
  <div class="container">
    <div class="icon">
      <img src="./images/icon.png" alt />
    </div>
    <div class="title">登录易卡拉权益</div>
    <div class="login">
      <van-form
        @submit="onSubmit"
        ref="form"
        :validate-first="true"
        @failed="onFailed"
        :show-error-message="false"
      >
        <van-field
          v-model="mobile"
          name="mobile"
          label="账号"
          placeholder="请输入账号"
          :rules="[
            { required: true, message: '请填写账号' },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请填写正确账号',
            },
          ]"
        />
        <van-field
          v-model="password"
          :type="passwordType"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
          bind:click-icon="switchPasswordType"
        >
          <template slot="right-icon">
            <van-icon
              class="showIcon"
              :name="passwordType == 'text' ? 'closed-eye' : 'eye-o'"
              @click="switchPasswordType"
            />
          </template>
        </van-field>
        <div style="margin: 16px">
          <van-button
            color="#1E1F26"
            round
            block
            type="info"
            native-type="submit"
          >
            登录
          </van-button>
          <div class="regist" @click="smsLogin">短信登录</div>
          <div class="tips">
            <div @click="forgetPw">忘记密码</div>
            <div @click="applyIn">申请入驻</div>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { login_post } from '@/api/app/seller/login'
import { Toast } from 'vant'
export default {
  data() {
    return {
      passwordType: 'password',
      mobile: '',
      password: '',
    }
  },
  methods: {
    onFailed(values) {
      if (Array.isArray(values.errors)) {
        Toast.fail(values.errors[0].message)
      }
    },
    onSubmit(values) {
      login_post({
        data: values,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      }).then(() => {
        this.$notify({
          message: '登录成功',
          type: 'success',
          onClose: () => {
            this.$router.push('/index')
          },
        })
      })
    },
    switchPasswordType() {
      this.passwordType =
        this.passwordType === 'password' ? 'text' : 'password'
    },
    forgetPw() {
      this.$router.push('/forgetpw')
    },
    smsLogin() {
      this.$router.push('/phonelogin')
    },
    applyIn() {
      this.$router.push('/apply')
    },
  },
}
</script>

<style lang="scss" src="./styles/index.scss" scoped></style>
